import Vue from 'vue'
import msgboxVue from './main.vue'

const MessageBoxConstructor = Vue.extend(msgboxVue)

const confirmCustom = {
  install (Vue) {
    let $vm = new MessageBoxConstructor({
      el: document.createElement('div')
    })
    document.body.appendChild($vm.$el)
    let defaultTitle = '提示'
    let defaultMaintext = '确认删除这条记录吗？'
    let defaultBtnGroup = [{ val: '取消', type: '' }, { val: '确定', type: 'primary' }]
    Vue.prototype.$confirmCustom = ({ title = defaultTitle, mainContent = defaultMaintext, content, btnGroup = defaultBtnGroup, needIcon = 'el-icon-warning' } = {}) => {
      $vm.title = title
      $vm.btnGroup = btnGroup
      $vm.needIcon = needIcon
      $vm.mainContent = mainContent
      $vm.content = content
      $vm.visible = true
      $vm.status = ''
      return new Promise((resolve, reject) => {
        $vm.$watch('status', (status) => {
          if (status === '') return
          if (status === 'cancel') {
            reject(new Error('cancel'))
          } else if (status === 'confirm') {
            resolve('confirm')
          }
          $vm.visible = false
        })
      })
    }
  }
}
Vue.use(confirmCustom)
